﻿using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace SubstrateHelper.Models
{
    public class ProcessRecord:RecordBase
    {
        public ProcessRecord()
        {
            PropertyChanged += (o, e) => {
                if (e.PropertyName.EndsWith("Value") || e.PropertyName == "InspectionState")
                {
                    UpdateFinishedState();                    
                }
            };
        }

        [Display(Name = "记录Id")]
        [Required(ErrorMessage = "不能为空！")]
        public int ProcessRecordID { get; set; }          

        
        


        [Display(Name = "参数分配号")]
        [Required(ErrorMessage = "参数分配号是必须的！")]
        public int ProcessParameterID { get; set; }

        public ProcessParameter ProcessParameter { get; set; }

        [Display(Name = "排产号")]
        [Required(ErrorMessage = "排产号是必须的！")]
        public int ScheduleID { get; set; }

        public Schedule Schedule { get; set; }


        private void UpdateFinishedState()
        {
            

            //
            if (ProcessParameter == null)
            {
                return;
            }

            //检验是否完成
            if ((ProcessParameter.InspectionType&0xfffe) != 0 && InspectionState == InspectionState.ToCheck)
            {
                //
                IsInspectionFinished = false;
                //所有未检的，为未完成
                IsFinished = false;

                return;
            }

            IsInspectionFinished = true;

            if (ProcessParameter.ValueType == ValueType.Text)
            {
                if (string.IsNullOrEmpty(StringValue))
                {
                    IsFinished = false;
                }
                else
                {
                    IsFinished = true;
                }
            }
            else if (ProcessParameter.ValueType == ValueType.Date || ProcessParameter.ValueType == ValueType.DateTime)
            {
                if (DateTimeValue == null)
                {
                    IsFinished = false;
                }
                else
                {
                    IsFinished = true;
                }
            }
            else
            {
                if (NumeralValue == null)
                {
                    IsFinished = false;
                }
                else
                {
                    IsFinished = true;
                }
            }
        }



    }
}
